<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">${shop != null ? '修改信息':'添加公司'}</h4>
</div>
<div class="modal-body">
    <form method="post">
        <div  id="error" style="color: red;margin-left: 40%;margin-bottom: 2%;display: none;"></div>
        <input type="hidden" name="id" value="${shop ? shop.id : null}">
        <div class="form-group">
            <div class="input-group">
                <label class="input-group-addon bg-color-x">公司名称</label>
                <input name="name" type="text" id="companyName" class="form-control"
                       value="${shop ? shop.name : null}" placeholder="公司名" onfocus="errorNone();">
            </div>
        </div>

        <div class="form-group input-group">
            <label class="input-group-addon bg-color-x">类&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;型</label>
            <select name="type" class="form-control">
                #for(Map.Entry et : shopTypeMap)
                <option value="${et.key}" ${!shop ?: (et.key.asInt() == shop.type ?'selected':'')}>${et.value}</option>
                #end
            </select>
        </div>
        <div class="form-group">
            <div class="input-group">
                <label class="input-group-addon bg-color-x">联系电话</label>
                <input name="tel" type="text" id="companyTel" class="form-control" maxlength="11"
                       value="${shop ? shop.tel : null}" placeholder="联系电话" onfocus="errorNone();">
            </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <label class="input-group-addon bg-color-x">公司邮箱</label>
                <input name="email" type="text" id="companyEmail" class="form-control"
                       value="${shop ? shop.email : null}" placeholder="公司邮箱" onfocus="errorNone();">
            </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <label class="input-group-addon bg-color-x">公司地址</label>
                <textarea style="resize: none;" name="address" id="companyAddr" class="form-control" onfocus="errorNone();">${shop ? shop.address : null}</textarea>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
    #if (shop != null)
    <button id="change" type="submit" class="btn btn-primary">修改</button>
    #else
    <button id="add" type="submit" class="btn btn-info">添加</button>
    #end
</div>
<script>
    $('#change').click(function (e) {
        e.preventDefault();
        var fd = $('form').serialize();
        if(checkCompanyName()&&checkPhone()&&checkEmail()&&checkAddr()){
            $.post('/shop/change', fd, function (data) {
                if (data.success) {
                    alert("修改成功！");
                    location.reload();
                } else {
                    alert("ERROR: " + data.msg);
                }
            });
        }
    });

    $('#add').click(function (e) {
        e.preventDefault();
        var fd = $('form').serialize();
        if(checkCompanyName()&&checkPhone()&&checkEmail()&&checkAddr()){
            $.post('/shop/add', fd, function (data) {
                if (data.success) {
                    alert("添加成功！");
                    location.reload();
                } else {
                    alert("ERROR: " + data.msg);
                }
            });
        }
    });
    function checkCompanyName() {
        if($('#companyName').val().length==''){
            document.getElementById('error').innerText="公司名不能为空！";
            document.getElementById('error').style.display='block';
            return 0;
        }
        else {
            return 1;
        }
    }
    function checkAddr() {
        if($('#companyAddr').val().length==''){
            document.getElementById('error').innerText="地址不能为空！";
            document.getElementById('error').style.display='block';
            return 0;
        }
        else {
            return 1;
        }
    }
    function checkPhone() {
        var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
        if (!myreg.test($('#companyTel').val())) {
            document.getElementById('error').innerText="手机号输入不正确！";
            document.getElementById('error').style.display='block';
            return 0;
        } else {
            return 1;
        }
    }
    function checkEmail(){
        var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //正则表达式
        var obj = document.getElementById("companyEmail"); //要验证的对象
        if(!reg.test(obj.value)){ //正则验证不通过，格式不对
            document.getElementById('error').innerText="邮箱输入不正确！";
            document.getElementById('error').style.display='block';
            return 0;
        }else{
            return 1;
        }
    }
    function errorNone(){
        document.getElementById('error').style.display='none';
    }
</script>